package com.bookcode.service;


import com.bookcode.dao.CourseStudentRepository;
import com.bookcode.entity.CourseStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseStudentService {

    @Autowired
    CourseStudentRepository courseStudentRepository;

    public boolean validCourse(Long courseId) {
        List<CourseStudent> courseStudents = courseStudentRepository.findAll();

        if (!courseStudents.isEmpty()) {


            for (int i = 0; i < courseStudents.size(); i++) {

                if (courseId.equals(courseStudents.get(i).getCourseId())) {
                    return true;
                }

            }
        }
        return false;
    }


    public boolean isStudentOfCourse(Long courseId, Long studentId) {

        List<CourseStudent> courseStudents = courseStudentRepository.findAll();

        if (!courseStudents.isEmpty()) {

            int i = 0;
            System.out.println(courseStudents.size());
            for (i = 0; i <= courseStudents.size(); i++) {

                if (courseId.equals(courseStudents.get(i).getCourseId())) {
                    if (studentId.equals(courseStudents.get(i).getStudentId())) {
                        return true;//学生选修了该课程
                    }
                }
            }
        }
        return false;
    }

    public void save(CourseStudent courseStudent) {
        courseStudentRepository.save(courseStudent);
    }

}
